var db = require('./utils/db');
var redis = require("./utils/redis")
var redisRpc = require("./utils/redisTaozi")
var mongoose = require("./model/db/mongoose");

const ClubUserModel =require("./model/ClubUserModel");
const ClubModel = require("./model/ClubModel")
db.init({
    HOST:'39.105.101.213',
    USER:'dfnodeuser',
    PSWD:'LT7Ndm24tkkaGRk',
    DB:'dfnode',
    PORT:5626,
})

redis.init({
    host : "39.105.101.213",
    port : "6118",
    password : "1OHu%F$XY5%ecC!V",
    db:2
})

redisRpc.init({
    host : "39.105.101.213",
    port : "6118",
    password : "1OHu%F$XY5%ecC!V",
    db:1
})

mongoose.init({
    url : "mongodb://101.201.78.207:27717/dafu",
    auth : {
        user : "dafuuser",
        pass : "fbL0qffPmCAgQNZa",
    }
})
//314778
//
async function test(){
    ClubUserModel.find({clubId:641945}, {userId:1},function(err, list){
        console.log(list.length);
        findUser(list, 0)
    });
}

function findUser(list ,index){
    let item = list[index];
    let userId = item.userId;
    db.get_user_data_by_userid(userId, function(data){
        if(!data) return;
        let mobile = data.mobile || data.real_telphone;
        if(mobile){
            console.log(index);
            console.log(data.userid+'    '+mobile);
        }
        findUser(list, ++index)
    })
}
const clubids = [ 194767,
    183168,
    634961,
    723627,
    734387,
    213245,
    989711,
    151867,
    151747,
    231692,
    111134,
    177936,
    494832,
    538435,
    151793,
    641945,
    814689,
    233353,
    268472,
    816575,
    953184,
    249827,
    713586,
    865419,
    585571,
    298742,
    639897,
    322244,
    198791,
    983277,
    245577,
    456475,
    574495,
    314778,
    754322,
    743696,
    756857,
    696952,
    442183,
    164164,
    914678,
    466876,
    784747,
    399691,
    537791,
    659631,
    485426,
    682488,
    222165,
    741941,
    966547,
    194533,
    795549,
    866181,
    642249,
    117432,
    869675,
    379891,
    241415,
    692661,
    291552,
    844738,
    583673,
    642687,
    135385,
    327813,
    384178,
    593115,
    972992,
    193248,
    195529,
    312166,
    828531,
    128931,
    732537,
    553716,
    111382,
    417929,
    274179,
    242288,
    526668,
    661939,
    223874,
    116499,
    968343,
    613427,
    882159,
    212825,
    256792,
    355432,
    188533,
    562166,
    115926,
    852964,
    623343,
    622487,
    321813,
    485973,
    312963,
    911542 ]
  

const fs = require("fs")
function writeFile(){
    fs.appendFileSync("亲友圈.xls","亲友圈Id\t圈主\t亲友圈名\t用户ID\t电话\t微信昵称\t几桌同开\t\n")
    let start = 0;
    setInterval(function(){
        ClubModel.find({clubId:clubids[start++]},{clubId:1,bossid:1,name:1,tablesid:1,configs:1},{sort:{games:-1}}, function(err,list){
            console.log(list);
            // ClubModel.find({clubId:231692},{clubId:1,bossid:1,name:1,tablesid:1,configs:1},{sort:{games:-1}}, function(err,list){
                findClubUser(list, 0)
            })
    },10000)
   
    // ClubModel.find({"configs.gt":{"$gt":1000}},{clubId:1,bossid:1,name:1,tablesid:1,configs:1},{sort:{games:-1}}, function(err,list){
    // // ClubModel.find({clubId:231692},{clubId:1,bossid:1,name:1,tablesid:1,configs:1},{sort:{games:-1}}, function(err,list){
    //     findClubUser(list, 0)
    // })
    function findClubUser(list, index){
        let clubInfo = list[index++]
        if(!clubInfo) return;
        
        clubInfo.configs = clubInfo.configs || {};
        if(clubInfo.configs.length>=0){
            let configs = {};
            for(let i = 0; i< clubInfo.configs.length; i++){
                configs[i] = clubInfo.configs[i];
                if(!configs[i]){
                    delete configs[i];
                }
            }
            clubInfo.configs = configs;
        }
        has = false;
        let configs = clubInfo.configs[0]
        for(let key in configs){
            let conf = configs[key];
            if(conf.bs>=0.5){
                has = true;
            }
        }
        console.log("查找亲友圈："+clubInfo.bossid+"   "+has);
        // if(clubInfo.clubId==231692){
        //     console.log("11111111111111:"+has)
        //     console.log(configs)
        // }
        if(!has){
            return findClubUser(list,index)
        }
        ClubUserModel.find({clubId:clubInfo.clubId}, {userId:1},function(err, users){
            writeFileUser(users,clubInfo, 0)
            findClubUser(list, index)
        });
    }
    function writeFileUser(list, clubInfo,index){
        let item = list[index];
        let userId = item.userId;
        let {name, bossid,clubId, tablesid} = clubInfo;
        db.get_user_data_by_userid(userId, function(data){
            if(!data) return;
            let mobile = data.mobile || data.real_telphone;
            if(mobile){
                let zhuozinum = 0;
                for(let i = 0; i < tablesid.length; i++){
                    if(tablesid[i]){
                        zhuozinum++;
                    }
                }
                if(zhuozinum>0){
                    let str = clubId+"\t"+bossid+"\t"+name+"\t"+userId+"\t"+mobile+"\t"+(data.name || "")+"\t"+zhuozinum+"\t\n";
                    console.log(str)
                    fs.appendFileSync("亲友圈.xls",str)
                }
            }
            if(index<=list.length-2){
                writeFileUser(list,clubInfo, ++index)
            }
        })
    }
}
writeFile();


function findClubs(){
    ClubModel.find({"configs.gt":1001},{clubId:1}, {sort:{games:-1}}, function(err, list){
        let clubids = list.map((item) => item.clubId)
        console.log(clubids);
    })
}

findClubs